package com.demo.listview.jquery;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class JavaScriptInterface {

	private Context mContext;
	private static final String TAG = "JavaScriptInterface";

	/** Instantiate the interface and set the context */
	JavaScriptInterface(Context c) {
		mContext = c;
	}

	/** Show a toast from the web page */
	public void showToast(String toast) {
		getCategoriesJSON();
		Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
	}

	public String getHelloAndroidText() {
		Log.d(TAG, "getHelloAndroidText");
		return "Message sent by Android Native";
	}

	public String getCategoriesJSON() {
		JSONObject pepe = new JSONObject();
		JSONObject obj = new JSONObject();
		
		try {
			obj.put("id", 123);
			obj.put("name", "Games");
			pepe.accumulate("categories", obj);
			
			obj = new JSONObject();
			obj.put("id", 456);
			obj.put("name", "Social");
			pepe.accumulate("categories", obj);
			
			obj = new JSONObject();
			obj.put("id", 789);
			obj.put("name", "Utilities");
			pepe.accumulate("categories", obj);

			Log.d(TAG, pepe.toString());
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return pepe.toString();
	}
}
